package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.ApiMapping;
import com.qfedu.openplateform.manager.service.ApiMappingService;
import com.qfedu.openplateform.manager.util.JsonResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sys/api_mapping")
public class ApiMappingController {
    @Autowired
    private ApiMappingService apiMappingService;
    @RequestMapping("/table")
    public JsonResultUtil findByPage(
            @RequestParam(value = "page",required = false,defaultValue = "1")Integer pageNum,
            @RequestParam(value = "limit",required = false,defaultValue = "5")Integer pageSize
    ){
        PageHelper.startPage(pageNum,pageSize);
        List<ApiMapping> userList = apiMappingService.getApiList();
        PageInfo<ApiMapping> pageInfo = new PageInfo<ApiMapping>(userList);
        JsonResultUtil jsonResultUtil = JsonResultUtil.success();
        jsonResultUtil.setCount(pageInfo.getTotal());
        jsonResultUtil.setData(pageInfo.getList());

        return jsonResultUtil;
    }

    @RequestMapping("/add")
    public AjaxMessage add(ApiMapping apiMapping){
        apiMappingService.addApi(apiMapping);
        System.out.println(apiMapping);
        return new AjaxMessage(true,"success");
    }
    @RequestMapping("/del")
    public AjaxMessage del(Integer[] ids){
        System.out.println(ids.length);
        apiMappingService.del(ids);
        return new AjaxMessage(true,"success");
    }
    @RequestMapping("/update")
    public AjaxMessage update(ApiMapping apiMapping){
        apiMappingService.update(apiMapping);
        return new AjaxMessage(true,"success");
    }
}
